diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs index d0900fc4f..e4d65d858 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs @@ -33,5 +33,17 @@ public enum StronglyTypedIdImplementations /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement an explicit cast + /// + ExplicitCast = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement an explicit cast + /// + ImplicitCast = 16, } } \ No newline at end of file diff --git a/src/StronglyTypedIds/EmbeddedSources.cs b/src/StronglyTypedIds/EmbeddedSources.cs index e1f229779..8580b10ec 100644 --- a/src/StronglyTypedIds/EmbeddedSources.cs +++ b/src/StronglyTypedIds/EmbeddedSources.cs @@ -25,6 +25,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_ImplicitCast.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_ExplicitCast.cs"), false ); @@ -37,6 +39,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_ImplicitCast.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_ExplicitCast.cs"), false ); @@ -49,6 +53,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_ImplicitCast.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_ExplicitCast.cs"), false ); @@ -61,6 +67,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_ImplicitCast.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_ExplicitCast.cs"), false ); @@ -73,6 +81,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_ImplicitCast.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_ExplicitCast.cs"), true ); @@ -85,6 +95,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_ImplicitCast.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_ExplicitCast.cs"), false ); @@ -117,6 +129,8 @@ public readonly struct ResourceCollection public string EfCoreValueConverter { get; } public string DapperTypeHandler { get; } public string Comparable { get; } + public string ImplicitCast { get; } + public string ExplicitCast { get; } public ResourceCollection( string header, @@ -127,6 +141,8 @@ public ResourceCollection( string efCoreValueConverter, string dapperTypeHandler, string comparable, + string implicitCast, + string explicitCast, bool nullableEnable) { BaseId = baseId; @@ -136,6 +152,8 @@ public ResourceCollection( EfCoreValueConverter = efCoreValueConverter; DapperTypeHandler = dapperTypeHandler; Comparable = comparable; + ImplicitCast = implicitCast; + ExplicitCast = explicitCast; NullableEnable = nullableEnable; Header = header; } diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs index e33515310..0abbfccc7 100644 --- a/src/StronglyTypedIds/SourceGenerationHelper.cs +++ b/src/StronglyTypedIds/SourceGenerationHelper.cs @@ -72,7 +72,14 @@ static string CreateId( var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable); var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable); + var useImplicitCast = implementations.IsSet(StronglyTypedIdImplementations.ImplicitCast); + var useExplicitCast = implementations.IsSet(StronglyTypedIdImplementations.ExplicitCast); + if (useImplicitCast && useExplicitCast) + { + throw new ArgumentException("Cannot use implicit cast and explicit cast at the same time. Chose only one.", nameof(implementations)); + } + var parentsCount = 0; sb ??= new StringBuilder(); @@ -132,6 +139,16 @@ static string CreateId( sb.AppendLine(resources.Comparable); } + if (useImplicitCast) + { + sb.AppendLine(resources.ImplicitCast); + } + + if (useExplicitCast) + { + sb.AppendLine(resources.ExplicitCast); + } + if (useEfCoreValueConverter) { sb.AppendLine(resources.EfCoreValueConverter); diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_ExplicitCast.cs b/src/StronglyTypedIds/Templates/Guid/Guid_ExplicitCast.cs new file mode 100644 index 000000000..fd32da369 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_ExplicitCast.cs @@ -0,0 +1,2 @@ + public static explicit operator System.Guid(TESTID id) => id.Value; + public static explicit operator TESTID(System.Guid value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_ImplicitCast.cs b/src/StronglyTypedIds/Templates/Guid/Guid_ImplicitCast.cs new file mode 100644 index 000000000..f2809f92e --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_ImplicitCast.cs @@ -0,0 +1,2 @@ + public static implicit operator System.Guid(TESTID id) => id.Value; + public static implicit operator TESTID(System.Guid value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Int/Int_ExplicitCast.cs b/src/StronglyTypedIds/Templates/Int/Int_ExplicitCast.cs new file mode 100644 index 000000000..e68aeb846 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Int/Int_ExplicitCast.cs @@ -0,0 +1,2 @@ + public static explicit operator int(TESTID id) => id.Value; + public static explicit operator TESTID(int value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Int/Int_ImplicitCast.cs b/src/StronglyTypedIds/Templates/Int/Int_ImplicitCast.cs new file mode 100644 index 000000000..3def768fb --- /dev/null +++ b/src/StronglyTypedIds/Templates/Int/Int_ImplicitCast.cs @@ -0,0 +1,2 @@ + public static implicit operator int(TESTID id) => id.Value; + public static implicit operator TESTID(int value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Long/Long_ExplicitCast.cs b/src/StronglyTypedIds/Templates/Long/Long_ExplicitCast.cs new file mode 100644 index 000000000..160156d24 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Long/Long_ExplicitCast.cs @@ -0,0 +1,2 @@ + public static explicit operator long(TESTID id) => id.Value; + public static explicit operator TESTID(long value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Long/Long_ImplicitCast.cs b/src/StronglyTypedIds/Templates/Long/Long_ImplicitCast.cs new file mode 100644 index 000000000..8d07cdc75 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Long/Long_ImplicitCast.cs @@ -0,0 +1,2 @@ + public static implicit operator long(TESTID id) => id.Value; + public static implicit operator TESTID(long value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_ExplicitCast.cs b/src/StronglyTypedIds/Templates/NewId/NewId_ExplicitCast.cs new file mode 100644 index 000000000..d31e6d93b --- /dev/null +++ b/src/StronglyTypedIds/Templates/NewId/NewId_ExplicitCast.cs @@ -0,0 +1,2 @@ + public static explicit operator MassTransit.NewId(TESTID id) => id.Value; + public static explicit operator TESTID(MassTransit.NewId value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_ImplicitCast.cs b/src/StronglyTypedIds/Templates/NewId/NewId_ImplicitCast.cs new file mode 100644 index 000000000..bf42c7a1a --- /dev/null +++ b/src/StronglyTypedIds/Templates/NewId/NewId_ImplicitCast.cs @@ -0,0 +1,2 @@ + public static implicit operator MassTransit.NewId(TESTID id) => id.Value; + public static implicit operator TESTID(MassTransit.NewId value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_ExplicitCast.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_ExplicitCast.cs new file mode 100644 index 000000000..72ee2f1b8 --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_ExplicitCast.cs @@ -0,0 +1,2 @@ + public static explicit operator string?(TESTID id) => id.Value; + public static explicit operator TESTID(string? value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_ImplicitCast.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_ImplicitCast.cs new file mode 100644 index 000000000..c5b1d4b94 --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_ImplicitCast.cs @@ -0,0 +1,2 @@ + public static implicit operator string?(TESTID id) => id.Value; + public static implicit operator TESTID(string? value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/String/String_ExplicitCast.cs b/src/StronglyTypedIds/Templates/String/String_ExplicitCast.cs new file mode 100644 index 000000000..5a37ae25e --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_ExplicitCast.cs @@ -0,0 +1,2 @@ + public static explicit operator string(TESTID id) => id.Value; + public static explicit operator TESTID(string value) => new(value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/String/String_ImplicitCast.cs b/src/StronglyTypedIds/Templates/String/String_ImplicitCast.cs new file mode 100644 index 000000000..e1f1bcc40 --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_ImplicitCast.cs @@ -0,0 +1,2 @@ + public static implicit operator string(TESTID id) => id.Value; + public static implicit operator TESTID(string value) => new(value); \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs index 2905f70e6..a594aef52 100644 --- a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs @@ -272,6 +272,23 @@ public void ImplementsInterfaces() #pragma warning restore 184 } + [Fact] + public void CanExplicitCast() + { + Assert.IsAssignableFrom((ExplicitCastGuidId)Guid.NewGuid()); + Assert.IsAssignableFrom((Guid)ExplicitCastGuidId.New()); + } + + [Fact] + public void CanImplicitCast() + { + ImplicitCastGuidId castedId = Guid.NewGuid(); + Assert.IsType(castedId); + + Guid castedGuid = ImplicitCastGuidId.New(); + Assert.IsType(castedGuid); + } + #if NET6_0_OR_GREATER [Fact] public void WhenConventionBasedEfCoreValueConverterUsesValueConverter() diff --git a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs index 9ad651d72..31e61a356 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs @@ -37,4 +37,10 @@ public partial struct EquatableGuidId { } [StronglyTypedId(implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableGuidId { } + + [StronglyTypedId(implementations: StronglyTypedIdImplementations.ExplicitCast)] + public partial struct ExplicitCastGuidId { } + + [StronglyTypedId(implementations: StronglyTypedIdImplementations.ImplicitCast)] + public partial struct ImplicitCastGuidId { } } \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/EnumHelper.cs b/test/StronglyTypedIds.Tests/EnumHelper.cs index c6eb61aa0..6a26fd0df 100644 --- a/test/StronglyTypedIds.Tests/EnumHelper.cs +++ b/test/StronglyTypedIds.Tests/EnumHelper.cs @@ -38,8 +38,15 @@ public static IEnumerable AllConverters(bool includeDe } } - public static IEnumerable AllImplementations(bool includeDefault = true, bool includeNone = true) + public static IEnumerable AllImplementations( + bool includeDefault = true, + bool includeNone = true, + StronglyTypedIdImplementations[] skip = null, + StronglyTypedIdImplementations[] only = null) { + skip ??= Array.Empty(); + only ??= Array.Empty(); + // get highest value var highestValue = Enum.GetValues(typeof(StronglyTypedIdImplementations)) .Cast() @@ -50,7 +57,9 @@ public static IEnumerable AllImplementations(boo { var implementations = (StronglyTypedIdImplementations)i; if (implementations.IsSet(StronglyTypedIdImplementations.Default) && !includeDefault - || implementations == StronglyTypedIdImplementations.None && !includeNone) + || implementations == StronglyTypedIdImplementations.None && !includeNone + || skip.Any(s => implementations.IsSet(s)) + || (only.Length > 0 && !only.Any(s => implementations.IsSet(s)))) { continue; } diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt index 76835ffec..3fc8c6b25 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt @@ -46,6 +46,18 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement an explicit cast + /// + ExplicitCast = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement an explicit cast + /// + ImplicitCast = 16, } } #endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..14b98bb1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..8eea6eb50 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=12.verified.txt @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..cfb7d0b40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=14.verified.txt @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..5b8624804 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..ea56c844e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..c3484248d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=20.verified.txt @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..8295511c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=22.verified.txt @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..2923ba496 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..1a81dab32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..2a0dc73dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..4eec31de5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..a9dde46f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..c150123c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..c74bada01 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..d8d2a3250 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..72c7a01d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..bb6b2634c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..984c91afe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..338d72fd9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..ed3a11493 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..937a9b8ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..eb51942d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..e9c1daeea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..7b2c65f35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..6f824f15b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..d3915ba22 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..ece313cfd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..e73283a0c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..cbbdb5ee7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..944873151 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..944780b29 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..3703a347d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..9777f2e07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..ef9d1d395 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..71286f454 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..46bbcd70e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..73648d060 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..63944f4aa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..d6d8fc8a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..bcbffc6a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..1a970778f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..c4166dd60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=12.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..b7ea4ed46 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=14.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..cbc3ef5c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..930731914 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..f35a93dd0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=20.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..4f2e755c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=22.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..cc2dc54fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..bb1a69eeb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..36506a1a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=12.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..698497a5a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=14.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..cbebab567 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..96186d703 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..be2b016c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=20.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..8f56a8e39 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=22.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..af2500b71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..f8dab421f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..482771abb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..20f2e8a2f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..044b45204 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..e5496b2bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..227337268 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..5cc7da999 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..9cfdab794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..909744a4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=10.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..16490dccb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=12.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..3e307fe09 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=14.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..da559088b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=16.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..b0493c99d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=18.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..4b4d4e61d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=20.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..d819ced76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=22.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..eaadbb4b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Long_implementations=8.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..7d6c17a07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..f561d21f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..6062892de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..16822ed52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..917962c83 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..dede41964 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..37a46a6f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..5af3956ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..901f3116a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..10b92ebdb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..4e5733da5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..e35d02268 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..8d557b33e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..08799149c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..207158bec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..241d3a24e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..4c65b0129 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=10.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..792223b9d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=12.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..ec4faf197 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=14.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..e1955a3b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=16.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..f32187d5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=18.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..d9e19bbe9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=20.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..3203783b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=22.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..5c033fe86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=String_implementations=8.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..11634e817 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..be8d7dbeb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..b4b3555e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..47d53f563 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..46790ef19 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..325a11006 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..0b16c7641 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..b45f29a53 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..446448988 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..a5af86762 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..e7ea9adc9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..2e95edcf1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..d4ef8925b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..24cd3dc51 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..032f45167 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..7107f0c9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..d39b0a6db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..4176bca1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..04d582995 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..af207d187 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..bb05da21c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..a3986b3d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..7cf5b7899 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..9e49ef94b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..5df7aa147 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..083a58a72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..33586cbb1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..4f7c36e7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..933c5241b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..7c39f970f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..c18e9ed4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..2afe738c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..871100a9b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..e659d2ca8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..472a6e3b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..d5d287583 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..ee6e987ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..9d3290b1f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..e9725f2de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..1f73b9d89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..9f4dc8851 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..51b635783 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=12.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..d6ca8cff3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=14.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..8c93b862b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..68039d0c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..6d0f70f97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=20.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..99f98c138 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=22.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..df9ce7ae7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..3521d48d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..d1c0d5edb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=12.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 + +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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..ee2b9fb5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=14.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 + +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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..0152cb175 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..b581c91c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..354853c6f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=20.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 + +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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..9a9ec541a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=22.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 + +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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..27ca0e457 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..0a1e6ad2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..d33817225 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..4d2044e4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..ad5c4bc6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..d823b3017 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..495efe2e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..87dcab9e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..f75ab25af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..3a7cd8520 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=10.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..e18b41426 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=12.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..9c3b48ebb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=14.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..2277872c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=16.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..45e94f052 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=18.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..59be107cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=20.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..062e2c06b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=22.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..2622c648c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Long_implementations=8.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..6f77e47fc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..f856ed9dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..d3bbbdfeb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..ccf6efc0a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..e5b7248d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..a02eec717 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..230976f30 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..0d2168e01 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..2502d1128 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..1fc19b974 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..b40343304 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..969387874 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..ffa9bf85f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..7a5334c37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..fda0141eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..277ffae64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..fd14536d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=10.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..e9985f636 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=12.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 + +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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..c280ef42f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=14.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 + +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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..da02f39eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=16.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..c06ddb2e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=18.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..931154b77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=20.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 + +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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..53696ce56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=22.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 + +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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..50b1937cd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=String_implementations=8.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..83ff68e0c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=10.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 +{ + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..6158506d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=12.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 +{ + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..a0c353ae1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=14.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 +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..7f8ccf96e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=16.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 +{ + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..a90db18fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=18.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 +{ + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..20da325eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=20.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 +{ + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..6db1111a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=22.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 +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..b01322a97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Guid_implementations=8.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 +{ + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..7fba49282 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..2fbce1391 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=12.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 +{ + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..c9a66f601 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=14.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 +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..8706fc7a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..932c0b4eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..83421cadc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=20.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 +{ + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..27fd1f2cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=22.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 +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..02c112814 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..80928f244 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..ff24ff1fc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=12.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 +{ + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..df09860c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=14.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 +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..a19b8ebfc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..48d520406 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..df9d5e05b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=20.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 +{ + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..ca1464f7d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=22.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 +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..5f24e6068 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..22dcde5c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=10.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 +{ + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..863003484 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=12.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 +{ + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..c76c10765 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=14.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 +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..5b6f35bb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=16.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 +{ + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..e99625b02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=18.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 +{ + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..ff4d9ef4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=20.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 +{ + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..ca1d90688 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=22.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 +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..6d43befe5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=MassTransitNewId_implementations=8.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 +{ + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..0c08fb28c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..3f1e4e1f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..c3644e0c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..7db4f1bd3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..edc312ae0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..8c3d77629 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..55fad7eea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..19c21d139 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..568354694 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=10.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..ac296cef1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=12.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..ffae5ad4a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=14.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..df4e7f104 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=16.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..75c1e54d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=18.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..abe4fa489 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=20.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..786521a99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=22.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..a1031850c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=String_implementations=8.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..a60d2ed12 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..31aa3e38e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..95370d3ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..e1f72e3f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..8f09e2bd1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..225af6b1b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..fdb2a107e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..8cfdba886 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Guid_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..3230435a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..10f1691bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..1aa3f128b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..403ee1f94 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..dc2630420 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..148dabf5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..76722fda7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..2073bd0e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..827fb6364 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..a78ec5f28 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..ed726fec3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..e2cd19603 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..a3eedd471 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..4b5b71cd9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..d2b9f46c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..3753d13b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Long_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..18573261c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=10.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..d798d22f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=12.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..37c561649 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=14.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..47ae1b636 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=16.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..d315ee3d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=18.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..0903dda3f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=20.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..7a80249bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=22.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..b8644d8e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=MassTransitNewId_implementations=8.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..0a6a1a588 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..8ebdcc870 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..f909a5f9d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..67e2e188d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..c6d807947 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..7705744e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..66b5ce466 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..0619eb847 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..7a2668ede --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..46adc49fc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=12.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..2563a9565 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=14.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..574fb74ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..e6383b046 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..2f00b3da4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=20.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..0b3f8bf52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=22.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..cecee9ebb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..a6485f417 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=10.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))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..852833758 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..deb8620ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..4f59e331e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=16.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))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..668c877d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=18.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))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..4c13c15f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..803efadad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..2dc1d219f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Guid_implementations=8.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))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..7e9f0a88b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..899dffe33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..e2d71caf6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..784310fd5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..21b165213 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..59a3a8181 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..d1b68ef2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..ce17ee160 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..e51921c11 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..f9db4a41a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..94c312284 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..72a442af7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..2d8123942 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..6dc4d595e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..ffdfd12e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..d859a439a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..3d6a2431a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=10.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))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..c0f566aa7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..df7ecb18d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..be730b585 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=16.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))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..325ee8e7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=18.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))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..be6500e05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..69788d293 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..961540d82 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=MassTransitNewId_implementations=8.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))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..b6f662a41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..63c897694 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..1f9c3f7e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..928fe7ff6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..6244591d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..2d5fce5eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..30b56a592 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..6d72cd8f2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..601042b49 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=10.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..f49c11e22 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..c8de4889a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..0d57f97f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=16.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..7f92b7652 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=18.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..71c1194b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..887f52f72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..0162a911e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=String_implementations=8.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..1ef9a5385 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..6bf561e7e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..b5bd09a24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..67f1032f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..074c01bd3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..08f987b3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..1d7dc9666 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..9ffef28d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Guid_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..a1aac96a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..c666f2386 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..a10106b4e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..39e109ad2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..5ea520c9b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..f1f8749bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..5a1ef4e1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..53f8c2e89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..8978d2498 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..7a874a7f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..3e1526e20 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..4382655d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..8fbe4587f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..ed9c63d25 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..a3a826f7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..7e786af64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Long_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..1193a4df1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=10.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..53785151b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..747dea8ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..8dded30df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=16.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..fdf285483 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=18.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..2da958530 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..4c4576e38 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..40356acc0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=MassTransitNewId_implementations=8.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..75a442c4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..0f42171ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..f4302585a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..d3b2b985e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..3b1382199 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..b0a9bc946 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..76aabd0d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..92a4d389d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..59b07a920 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=10.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..35556614c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..6d0cf358f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..69156e7dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=16.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..2e29ebfa3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=18.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..b9616da04 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..12d461269 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..4af769744 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=String_implementations=8.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..81f9d19f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..6a6bd806c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=12.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..0a6cda03a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=14.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..88a29fe85 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..55bf0f433 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..7c418446d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=20.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..91ffb8502 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=22.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..78a17acc7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..d34692e84 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..97b4028f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..d2e7e30de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..08b9c42f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..1adb0c2f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..773e9fc41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..fa8e274d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..cd97e3788 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..b350f040b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..4879c1b56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..253ab8b28 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..b6ce69efc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..5b6a1e8de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..4bd2aba97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..b227c27b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..c34bc9f39 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..aeffb1202 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..3b423eba7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..81d6a317a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..77c2a5b15 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..6adac5599 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..4961779f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..ceced9d17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..ab0f39ba9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..a9b7ea3ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..de25f3ed1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..c199d0394 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..d1333211d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..375c17062 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..0527ed900 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..13409c63c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..98c63cfa4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..ae3c5836f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=10.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..5d02c0ec8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=12.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..4c6b35001 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=14.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..97ad593ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=16.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..f2bbb6c41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=18.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..01aca2b5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=20.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..3518bd743 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=22.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..da0b8955d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=String_implementations=8.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..9e8a69569 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..daa364af2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=12.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..dbdcac274 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=14.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..ebf14cc60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..c83baac1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..d69ce4444 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=20.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..dae3a31f2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=22.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..0dae8fbe3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..7156c444f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..ba71687a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..0e8696653 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..7895c559e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..81d1730b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..2def00889 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..54309d0f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..69e8b290c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..f8d1ccfeb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..7538efb92 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=12.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..383fe50b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=14.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..27245d2e5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..c9b2503f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..b94b3890e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=20.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..a852758d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=22.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..c1f6f3d41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..4e6bc74c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=10.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..481c673be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..e6aef8b68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..344955e04 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=16.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..55b815635 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=18.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..08ea94754 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..3bfc4e454 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..5c67303d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=MassTransitNewId_implementations=8.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..e8dc62831 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..b382a3834 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..84cc67eb3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..a044a3bc7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..60b3deff3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..e3a22e452 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..8464f1ce9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..7c55510e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..0cf8c2821 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..d00fa4362 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=12.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..9823c6a7f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=14.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..038c720a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..55f2c4c5b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..a7b7da711 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=20.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..67cb700a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=22.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..623cc9eac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..312d02b92 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..26b03417e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=12.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..bf2875515 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=14.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..1eda3db17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..a689810ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..13cadf1b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=20.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..2ff8221ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=22.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..e1fbfdead --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..8b26c2404 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..03af9fe03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..f3155baf0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..ea28895c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..84cf448db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..d6dc4bbe1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..26bb8861e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..485cfd728 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..ef3614307 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..9746e81c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..5a7f1259f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..5e1f9bc61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..8fc520598 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..ae619a40e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..3b1ad5426 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..70aafdde9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..334d2da25 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..ef4f03a9c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..ec13b8c9e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..51a7026b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..1041e69f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..7cbcf63d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..974112b46 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..fb58ad97c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..ce91a5d93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..1160ba965 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..dd6c80448 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..d31d7f228 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..2d87f3e08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..99482af91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..3e0444f0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..b9795de87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..358ccc925 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=10.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..962803b0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..d9aba9dba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..03361e11c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=16.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..f662a8ba1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=18.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..152698f99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..338013188 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..e45517d15 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=String_implementations=8.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..78c82832b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=10.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..d33329cd9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=12.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..9231e1e85 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=14.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..807b6cfb1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=16.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..ff2a5c799 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=18.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..046ae2728 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=20.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..bb9593667 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=22.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..9c07bd836 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Guid_implementations=8.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..9b8c5fab6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=10.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..c3976fbd9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=12.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..2923219e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=14.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..c2d260aa6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=16.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..64e4d99a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=18.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..0cd9cd55d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=20.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..bdab83209 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=22.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..e8330a03b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Int_implementations=8.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..855b1b99c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..b37b62d37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..e41430e6f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..2823a0d03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..46ef73df4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..b3d3dccc9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..649ffec8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..48cf8f8c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..b7796553d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=10.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..f310acdfa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..c6038d6cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..faec0397d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=16.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..c93e956ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=18.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..732862b37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..54a846eee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..f294b91de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=MassTransitNewId_implementations=8.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..8c7197033 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..8f082b22c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..bc8a3110a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..a14ce599f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..6a172feb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..79ee7fb87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..b1aa838f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..ec9d2d3e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..3dad5d1a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=10.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..73d69a94c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=12.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..d82ea92c6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=14.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..e2132d713 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=16.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..fd400787a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=18.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..f5729106d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=20.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..cdb7c8716 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=22.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..6a0c93af9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=String_implementations=8.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..ae0d76c7e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..fd5a1624c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=12.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..b58546881 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=14.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..edff1804f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..2c8804853 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..67b011421 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=20.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..8fedb795a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=22.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..5eebf2c16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..5d0dd21ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..eedf3765c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..705a58bed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..fc4dbaf95 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..2af924adb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..6ddeb088b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..acfe1c23a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..f60bfd782 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..eddbbdaa0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..1096dee02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=12.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..dc16f685a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=14.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..d15dad52b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..9e776c25d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..acc66f9ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=20.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..e44fbcc01 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=22.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 + +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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..4a7225663 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..b9359ab56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=10.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..8d1dcdbec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..8d85cdf6d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..55c580f22 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=16.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..b5ab98529 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=18.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..6a1af740f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..2a10f2585 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..9033ce293 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=MassTransitNewId_implementations=8.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..052e561e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,151 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..85149801d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,161 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..94b1e8583 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,161 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..7dbc88b30 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,151 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..af67de29b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,151 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..18a2f6877 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,161 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..318b21bd9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,161 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..566e3c3ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,151 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..2b0928b74 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=10.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 + +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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..a202bcee6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=12.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..3fe0f39b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=14.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..aca921a9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=16.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 + +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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..6d35fe29a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=18.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 + +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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..5c542ae99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=20.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..753d697f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=22.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..e86e76d8c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=String_implementations=8.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 + +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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..ed295c088 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..1e10db782 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..3e10ff4e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..d95ec9313 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..6851cfc52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..6b64268fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..bc66bdeb7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..933e05bcf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..275a72ba5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..5561f5981 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..49891e98b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..0c8cf12bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..461044007 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..d4a566ef0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..39d49eaeb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..407cc7470 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..dad864fa4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..b8258ff50 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=12.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..65f24d76d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=14.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..d40df929b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..5fe657238 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..1b399b427 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=20.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..2bbcae5d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=22.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..84f26d3d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..a8dbe9890 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=10.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..23b445dd5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=12.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..29c90ba05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=14.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..b38b1305f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=16.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..8e562df7b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=18.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..ee7a215ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=20.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..cf5c78b98 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=22.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..b94f886e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=MassTransitNewId_implementations=8.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..54db59504 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..eaa2f4301 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..d76084bca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..a47d74ac1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..d5873cc7b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..ce036ad71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..2b27ebfd2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..068266a21 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..97a60b301 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=10.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..8d19ce847 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=12.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..8b819d629 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=14.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..1baffc16b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=16.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..447753d48 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=18.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..5d501572f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=20.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..5414a62f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=22.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..7cda598d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=String_implementations=8.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..205d10013 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..acb11a1c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..a474c4a72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..b3394f3b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..e75d65ff9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..a1b5252e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..c1eefa227 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..edde03bf3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..b106f4fc5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..fecebf3a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..cc7a4aa93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..0479f7a7f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..02da24fe1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..4cc20ff66 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..37d80735d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..ed3915f6d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..1c22e57fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=10.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..3f8ae0097 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..4208124ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..c81c922af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=16.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..a28c7ab2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=18.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..62e0c3f8a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..55b3aa7c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..c46cce58b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Long_implementations=8.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..aa2d0728b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..7e0990b1c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=12.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..86daa88f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=14.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..bdb70b7e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..035f9509f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..5d2dfee08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=20.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..e13caf53e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=22.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..b309aba7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..a2e53368f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..e76315f13 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..c8ec33a4e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..8fadc1b68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..133307e39 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..658afb7e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..9c9a02001 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..1528582e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..c9ba0627c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..5e950a5c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=12.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..10db19d82 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=14.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..eee9a6c67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..19fb8a398 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..a575cb418 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=20.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..a6223ac54 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=22.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..10fc0806e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..bf4de29f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..9f35e0b9c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..2746e600a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..e1cc7108b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..9e6291eb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..c05c40d9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..66bf5f8b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..2f88dd361 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..407cf48f2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..1423392de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..14eef1c03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..33f0f1ca5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..8849d5e55 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..94e13e4a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..ca31d6b65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..7a225912f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..9030a19d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..827f72137 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..706076e32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..6a5920b7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..775cefe00 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..2be340579 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..35799b745 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..264db27b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..52a16679e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=10.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..4732f8830 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=12.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..d11e55cb2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=14.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..098d5df78 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=16.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..f66cba8e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=18.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..4566e9be0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=20.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..3bc41315b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=22.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..8042919e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=MassTransitNewId_implementations=8.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..42571ed0c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..a7c81da9e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..42d969ae5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..54b496687 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..667fce084 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..30ad9f86a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..3f51cd616 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..099a8c5a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..b6178b08f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=10.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))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..caf49f5ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=12.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..41251a7b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=14.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..a7ca8cc65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=16.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))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..9b1a15482 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=18.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))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..0d5100e36 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=20.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..5deea52a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=22.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..2aa5769f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=String_implementations=8.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))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..c23644286 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..41791df89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..92697bcd3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..f148ab65a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..4b7971cc7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..d9bdd3177 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..735d3eb07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..27febd34a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..ec2d0bea1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..ef7233038 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..be86f47cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..52e662eb7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..68143d989 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..d34e7fbe1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..7ba3a617f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..df3072281 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..933cd338c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..cb0ed6916 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..4d45c9a33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..ccc1e8da5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..a92b0d092 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..652db1c62 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..b89f4e804 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..f36e1ae46 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Long_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..dfbb525a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..ebfc1143d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..69e56bad7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..ebefedaba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..c26def02d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..8030078e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..91cb577be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..71664a381 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..5ef02bd57 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..a9a768caa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..0229b2f3c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..6b9c1cb5a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..26dbdbf97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..efb62bdbd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..e1d17d3ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..b4673a1a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..2bd7b9062 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..253823ca7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=12.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..0dbdeda1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=14.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..e600dbc40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..2ab44822e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..a8c4a19b3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=20.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..ad2f622e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=22.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..17dc7b336 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..9df87d679 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..7cc3b04a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..b45250221 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..9661d4aa8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..5d42e0301 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..45825d7a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..9e5f6384f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..559eeebb7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..3868437d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..92821b14c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..a02c9488c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..a6d108861 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..092bfbfcf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..80342f4fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..7818bfd9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..08edf1d5e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..8f6e0e6eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..b9eeef461 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..0674ea039 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..f2acc9b10 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..65a879474 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..fcd8a0e9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..aebb5b222 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..170b5e5ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..30ef54929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=10.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..001029994 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=12.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..df6fabf97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=14.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..5406a46b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=16.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..b769c2451 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=18.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..4dfcf932f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=20.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..138013079 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=22.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..2cee2aa5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=MassTransitNewId_implementations=8.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..54c705a28 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..075caebf2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..37ec59e8a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..4ef8d346b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..7e7465964 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..60636ed6c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..0522834f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..511b143cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..a78854995 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=10.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..e0e3fd31d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=12.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..4b1a0beda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=14.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..a622352a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=16.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..67ae99de0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=18.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..8a7e8350e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=20.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..c8f88eb4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=22.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..80bac2b55 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=String_implementations=8.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..9b4e27018 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..0934e508b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..10cd0e02b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..217808442 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..1e1d30d26 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..53454e976 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..331672164 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..3ab08c559 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..986f008d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..4c0899e3d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..af3f8cdf9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..e504585a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..d419c6c3d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..d44ec2d98 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..9baab4eee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..46f757b56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..c51ea1568 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..8e2a39c10 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=12.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..435ceb7d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=14.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..3fcc67b89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..5fa39dce0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..0de8d725b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=20.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..8f1a96abb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=22.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..b756eb68c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..62717f008 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=10.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..2a2a4215b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=12.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..7caddc665 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=14.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..b24cdd265 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=16.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..3bf39ab42 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=18.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..6c10ba13d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=20.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..f132c4894 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=22.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..b8e013b3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=MassTransitNewId_implementations=8.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..6fe710456 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..e295539c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..4a3585d89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..58bcb007b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..f161354d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..65b698b4a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..e3a907a65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..507cc2f58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..1b41815c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=10.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..d28507439 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=12.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..b3e24f2b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=14.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..84f7d722f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=16.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..c17081b1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=18.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..4a00d4950 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=20.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..77b8eb30a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=22.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..cd20af51f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=String_implementations=8.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..f398860d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..7c4339628 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..63295ff68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..85af5d206 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..0b1608f67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..6e1b76e33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..bdf3ea977 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..010a6c232 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..c80ec2a5a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..10266af77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..caf07bb91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..5466e838e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..8bed4767b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..8642019bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..f574801b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..bbd48ae94 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..7dc2996cd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..f5815c83a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..6be471a95 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..656e5bdbe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..8547a7ddc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..751624722 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..6533c271a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..8e52f07f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..eaaa1b7f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=10.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..eb091b7fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=12.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..5f5dd075d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=14.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..31b179428 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=16.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..33de8b99c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=18.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..c7f16422a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=20.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..618b99db6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=22.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..ecc95895c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=MassTransitNewId_implementations=8.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..f67a56adf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..475490617 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..543d92b0c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..677bed9db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..546af86f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..d5e62dc2f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..a56b00aa7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..c3ab9fd35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..af327c659 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=10.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..d186974bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..58a175c19 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..abade9674 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=16.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..aa11c4e42 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=18.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..6a513b598 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..d9a30fb4e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..6eef87a4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=String_implementations=8.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..2693a1ed6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..f8a1e8729 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..cd2d4124a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..8facaa053 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..e32b9d208 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..3192dd6b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..3c8b01112 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..789704214 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..277865433 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..c44be586a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..91380aa0a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..46b66454d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..f389ef6c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..8f219fca3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..1ead94fa8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..eeac6fcf0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..1b05380af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..43a582b56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..598d41168 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..e25f014c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..7840c4aac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..c389598d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..14f1310f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..cce2dce7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..f035d14db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..de313ba32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=12.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 + +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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..030d37b0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=14.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 + +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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..c60781b8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..238b99ce1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..10994250b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=20.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 + +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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..a94cfd376 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=22.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 + +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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..f39f81585 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..3bcac4e6d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..dd26625d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,169 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..f375b7fda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,169 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..c08044cae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..4808f9124 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..0ba71adaf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,169 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..53849227a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,169 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..456ca46fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..b9dfb9b14 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=10.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..9663f0d58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=12.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..e7cf9e39e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=14.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..5c4ae7990 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=16.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..dfd221cc8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=18.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..75055b927 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=20.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..24ee4de27 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=22.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..c19c7f212 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=String_implementations=8.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..e0e1942af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=10.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..9d1d9bbf1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..af4fd2a33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..013c1a25d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=16.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..d403198fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=18.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..b743bd657 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..1095421e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..b050e2117 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Guid_implementations=8.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..6b46ea553 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=10.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..d6040a403 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..efbffb7b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..7ad50b585 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=16.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..aea47d1be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=18.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..45805e84d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..1897ff4d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..23dbff8c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Int_implementations=8.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..d981ddcf5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=10.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..12d9889cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=12.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..ed3936927 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=14.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..707246564 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=16.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..265cfaac3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=18.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..5966e8bee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=20.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..1a7fdf1c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=22.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..915b57fc5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Long_implementations=8.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..d91e30491 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=10.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..7812e87df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..a06c9d244 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..29e4b0127 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=16.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..2233ce93b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=18.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..5c21e80b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..1b0f308b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..e49689cef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=MassTransitNewId_implementations=8.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..03d750c0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..123f15178 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..ff587f1f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..fdb28fbcf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..1b09efd00 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..9dee22aed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..ebc4cdeaf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..1e8a0d7f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..042499259 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..691e671b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=12.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..856bd6036 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=14.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..793559878 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..0e64962dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..5bcb7aef2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=20.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..8aa060364 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=22.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..5cd12c7cc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..59410f13f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..d6bb9008f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=12.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..be134237d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=14.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..522bee14b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..85198ebae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..893854226 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=20.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..c8d9cab8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=22.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..aca1aa609 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Guid_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..6c1768d81 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..915b23f53 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..43252e0ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..f46affd9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..5454a4f99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..0a05c4729 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..c93af47bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..0d7627a43 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Int_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..6700dd17a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..38dd8e87b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..51c67c320 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..bcd09fdb6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..33293cdec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..1ed2efe21 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..9bec87dc3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..f02d55098 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Long_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..348665116 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..f37297379 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..233d0863a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..f7458a11f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..12cbbd776 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..33536c7af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..ee42f1eb1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..707dbc542 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=MassTransitNewId_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..0045102fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..e8fce9d3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..38b28c281 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..0b8a041dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..13318436d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..26120e47f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..02e875008 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..a9dbe439e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..89125215d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..a151dfe86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=12.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..70db6d013 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=14.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..ae2f658fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..d573d2819 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..f7daea4a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=20.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..b584be81b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=22.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..98fcdb33b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..7e758cd74 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=10.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..a03d84da6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..2c7e78868 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..5305fe34f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=16.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..353a37f5b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=18.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..af53e9c2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..b9f32755e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..831920606 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Guid_implementations=8.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..ddb3eb4db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=10.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..b6dda8a27 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..6548531fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..7ef73fa6f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=16.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..6db427886 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=18.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..a175b8b99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..ac84cb441 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..aa2760182 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Int_implementations=8.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..fe5f95d9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..546baa102 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..40b91d28e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..7ff56001e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..53bb70ad2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..337eb07c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..c9c7fa348 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..eb47159f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Long_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..b22157b3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=10.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..731bcdaf5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..0d146f76f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..c24630ed8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=16.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..72d6af50c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=18.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..bf31854d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..c08c5192b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..fc88af45f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=MassTransitNewId_implementations=8.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..badf39a0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..014a7b0b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..b25993f8c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..f589e4c92 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..d7578984d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..ce2ea64f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..1060a876a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..92b72ab3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..659ed9863 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..a06969dba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=12.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..3851b12ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=14.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..a8dd22edf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..b8a7becb2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..e99e7b96b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=20.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..39b99f053 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=22.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 + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..03928ab81 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=String_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..0c5974247 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..1299e6ab7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..e5c090a7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..ca7523ab2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..3df471556 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..73feb1c14 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..194f8fd33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..e05d4e314 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Guid_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..0328fb498 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..8459f6856 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..2c56a6842 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..fe41e576e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..149d15ed1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..c1035fcef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..e669b7e0f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..6b5916f84 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Int_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..20e4bdf05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=10.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..a36f4e1fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=12.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..165c96af1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=14.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..fce0ce968 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=16.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..fdf5e406e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=18.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..af7bd5d06 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=20.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..5d19cf57e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=22.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..3bfe567b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Long_implementations=8.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..32be21fba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..617c926f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..043f9664f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..da9941f1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..dfa56cbb3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..9f1f4a0a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..1fdc71ed6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..faeee4325 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=MassTransitNewId_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..dbc918a54 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..533c9208d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..20eae8d7f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..6fa4dc494 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..a17a881c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..8b7e27fbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..e11106d78 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..8e3e1498a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..563e88a36 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=10.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..76947e800 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..9a2b59cf6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..7db17a602 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=16.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..25db19ff7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=18.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..5a010befc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..6c9be9d24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..af81ddd62 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=String_implementations=8.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..b90e28864 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..9e927e616 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..40433bd35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..7f91fb5f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..70e08ed41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..942d64c9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..348947386 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..3c6bd5ffe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Guid_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..8e6b23a7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..0e282369b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..073b02d19 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..8f9c228af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..1dd0f9c63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..71db8b315 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..9243c8187 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..1fbc6fe22 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Int_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..0b6c6859b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..55e545a9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..c46f74e16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..ecae25f3f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..a6d0aa469 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..0484e0463 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..05345efb7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..15770c103 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Long_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..d446da3bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..df5905750 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..4ae8371ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..01be93372 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..986a2eee4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..6b8a852ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..ac516cf8c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..ba9f3336f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..675f3f1c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..284fa5cf3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..b187213fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..cc53fd518 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..a439afcba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..bb15a0ed4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..1d14e92f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..af07411c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..588ffde54 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=10.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..08b9a2575 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..11ce9dc8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..1cdee64d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=16.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..ecc99b014 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=18.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..ab47822b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..5a40e8410 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..01242ecce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=String_implementations=8.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..850b7989d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..44da8aa61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..03c47ad70 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..e95ab89bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..fd6c0cd0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..e744f5f2f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..133325099 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..154564afe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..bfd5a9a68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..124debed7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..b372da7b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..a8ad8eabd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..0af4279ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..a91c1b936 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..77929e61a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..10fccdfc5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..8a7fbca8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=10.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..4a06e7dfb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=12.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..53812e027 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=14.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..2709f8092 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=16.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..a269f8172 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=18.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..7a20a824c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=20.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..4e090cdec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=22.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..64faf3e11 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Long_implementations=8.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..f1ce0af2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..1723badf3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..068b7070b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..3752640f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..f6d444394 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..e076cb04b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..b6da4bf1d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..cff45a025 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..8cf5bfd61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..da818cf90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..503ab2ee5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..5b36b5c05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..ab27a2f78 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..2de76617b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..77470425b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..da8c3ed90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..fde1c6e70 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..6e3ebc8b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=12.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..36eac7ed6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=14.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..6cb850849 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..bc5d29a96 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..821a9b92f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=20.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..a5be33108 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=22.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..9f8508bdd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..12fa71168 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..8cfb0f000 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..2e63bfcce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..548e746cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..3db972561 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..10142e24a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..8a28166fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..cb4fc8da5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..6d469c3d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..08187e256 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..1f576fa87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..9a03baf7e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..b0bf4d618 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..591799d9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..2da325214 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..a12699e64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..3c099b390 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..7c6cb33a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..177950110 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..422b92fb8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..aaff1cb6e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..62be282b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..79f3bd9e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..6e5c42e17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..43b5abac2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=10.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..8eb017556 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=12.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..2fe21dc16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=14.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..3d1cf6844 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=16.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..d6f70b46f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=18.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..797d3d3e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=20.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..d245664b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=22.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..ef43e8584 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=MassTransitNewId_implementations=8.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..750cc8310 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,143 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..6747b0efa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..8bf6a2589 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..2d8e02fda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,143 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..f192fb114 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,143 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..e3969dd54 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..6c6070980 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..63417d2c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,143 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..cd1f7968e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=10.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..da6153b12 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=12.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..0bb351b01 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=14.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..d0b1a066c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=16.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..b2c11570f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=18.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..de95e3821 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=20.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..f304f8619 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=22.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..c0dba0c72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=String_implementations=8.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..1bd5ae8b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..e05785293 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..3b15037e5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..df9bcd6ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..e377ccbd4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..c537de63f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..9b0add2f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..c6a5d1823 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..ffcb8c2d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..122ffdfa8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..de4578f14 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..7e9d56309 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..2e777e3e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..adc293294 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..8d6f32b7b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..160649aed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..4d4f4ea7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=10.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..4c6565b75 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=12.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..b83aca456 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=14.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..64223a1a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=16.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..3b4ff896b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=18.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..b920de28e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=20.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..61f2e28c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=22.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..b7ca6bed8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Long_implementations=8.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..f43f77607 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..8164910ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..4481eb56f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..962fe657f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..18d3b1138 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..4cef23be7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..7d1435b60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..cd6494164 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..ac2cad556 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..4b06d0b72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..133052151 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..1f48b71e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..85e03f194 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..43e3abf84 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..aea3f4973 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..19be14011 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..692a048f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..27895bed2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=12.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..a1be0a7b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=14.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..47990be7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..42c8f1e80 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..03dcba0ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=20.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..45b69b266 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=22.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..5db5598ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=String_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..0670300e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..f4b445f89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..bb96b2e29 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..d4243e32e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..847758e7e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..a0dba5f02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..3eac30421 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..f7a81329f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..ca0dfc82e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..2f253a872 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..78c14781f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..b9310a732 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..a4b5d69dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..d36df9c6c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..4342d00ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..e7412cf83 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..ea40ed152 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..e1bc0365b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,151 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..042fdf1b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,151 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..13d2051fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..574ed64ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..874f514e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,151 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..e556384f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,151 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..9737bd9f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..7ddb70cad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,155 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..5697cf7be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..20b1d9194 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..05d731e44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,155 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..2b97616ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,155 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..651dc4ab3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..a31dfd5d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..d9cace853 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,155 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..31db1fad2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,170 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..ec5b97424 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,180 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..a98659e2c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,180 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..d23a63db3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,170 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..8eb3416da --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,170 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..47697780b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,180 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..f1c5e8699 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,180 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..63096b3bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,170 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..19d6bbd58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=10.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..be08b061d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=12.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..23f5d362a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=14.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..3d3b80b09 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=16.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..ca45bb0bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=18.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..be98ce528 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=20.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..4764e87ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=22.verified.txt @@ -0,0 +1,159 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..f11992216 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=String_implementations=8.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..494fbfc96 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..367564a51 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=12.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..fd9e1d6c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=14.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..a9cbb25af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..80d5ae43c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..b1dfb5871 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=20.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..b9b6112fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=22.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..129ae4965 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Guid_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..34609e049 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..64602c855 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=12.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..56aaeedc7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=14.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..ff099ffe4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..1b0ab3816 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..d59d0e505 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=20.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..690dcf1c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=22.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..95d81b3ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Int_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..8af6b69d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..abb6e4777 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..5409f6794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..982ee8766 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..09827a65e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..1eabc08c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..b956f8144 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..c1bd6d766 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..e928830e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=10.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..205df1105 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..dd6eb31c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..40cb7f25f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=16.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..e62ba5b69 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=18.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..bcdd870ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..6745538f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..590577dad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=MassTransitNewId_implementations=8.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..e1ddc602b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..7a1608edf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..ec6f35a32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..f13ac9dbd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..340588ecc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..1a6c30972 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..00f4b098a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..5c86b61e5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..e54a1a2b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..904139855 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=12.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..38f4d175a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=14.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..315c0d332 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..13e1f8b02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..b36fab099 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=20.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..8c790e730 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=22.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..19c59725d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=String_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..04c63a4f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..75c759730 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=12.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..6b9c400a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=14.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..bd145a356 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..7607eaf77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..f767fd789 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=20.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..351421d17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=22.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..cd95c5c89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..44080c5cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..326cb802e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..70d97b2e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..72d703de2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..3a6ce17ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..4507c684e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..d0ba4c8d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..ca0114c6c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..21c4243ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..9dc75bc99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..052db1a3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..7e4371071 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..bcb7fc895 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..0e759fac5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..cb52b6dcb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..0091eb4e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..f96164fbd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..99b969318 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=12.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..24517fd98 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=14.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..43214abec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..ff14bece8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..820f6e849 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=20.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..c98ca273f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=22.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..f25481f4c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..ac08a157a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=10.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..4739749cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=12.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..03c891a71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=14.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..bc73dcd31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=16.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..97fe0cb65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=18.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..4faead82d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=20.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..7d02d5809 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=22.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..fdd345e34 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=NullableString_implementations=8.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..458f10c71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=10.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..04762f68b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=12.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..b5481290a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=14.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..e17a3c6bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=16.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..5c6f23a6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=18.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..7194fadce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=20.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..062d1c012 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=22.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..1b5b4063a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=String_implementations=8.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..9ca277927 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=10.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 + + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..a47bc5cd2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=12.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 + + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..518c9b23b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=14.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..ea848108f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=16.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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..fb2f1e880 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=18.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 + + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..8afd906cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=20.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 + + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..92273574d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=22.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..3ec001ffb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Guid_implementations=8.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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..49dc3d3d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..eae0f080b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=12.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 + + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..e62a049b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=14.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..58fce6db8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..6271633a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..3a032936d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=20.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 + + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..ec57d3326 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=22.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..650d91bdd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..6d522923e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..356bfd564 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=12.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 + + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..369229080 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=14.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..2c6e3343e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..048e216bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..586768cce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=20.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 + + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..66addfaa4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=22.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..2cbf3d801 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..ea1b4f614 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=10.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 + + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..75b991423 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=12.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 + + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..9c8b7a0b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=14.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..92a2c3ecf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=16.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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..2a51d5577 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=18.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 + + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..e7cdd8833 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=20.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 + + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..dbe363247 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=22.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..0646f05ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=MassTransitNewId_implementations=8.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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..af3d9d36a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..3fff29fe6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=12.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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..7274a15d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=14.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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..201e723f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..edad20759 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..5cd112c44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=20.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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..e6edc3aa9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=22.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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..cd5b1f065 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..2f49324ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=10.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..1112e8484 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=12.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..9f95bf423 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=14.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..369eeefe5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=16.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..9c99c1104 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=18.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..ef01d4e0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=20.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..db90143aa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=22.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..6f7c6dab1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=String_implementations=8.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..aec2ba543 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..914fe47ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..0db9fd601 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..9a9181b03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..423f1dde7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..2ef819934 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..710c328dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..6297dedbd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Guid_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..5d832988d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..284ea6593 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..721e5ec09 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..334397429 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..713d620fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..6e2d528ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..3f24f5552 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..313a01e27 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Int_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..82cf0d060 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..761bd7020 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..5ae1b8ac7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..e13ba01e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..c5676d441 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..dc2d1450a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..70031ad4a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..2637023d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Long_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..a21527763 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..efb7696db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..c0d04f676 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..5fd47daf7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..bc0a31d62 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..08c1daa12 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..16941c49b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..c8b24c789 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=MassTransitNewId_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..98a8bc226 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=10.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..0d8949f92 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=12.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..905043071 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=14.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..42bca2fe0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=16.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..b4c5a444e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=18.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..866554b68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=20.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..b9d469463 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=22.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..c0bdc57fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=NullableString_implementations=8.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..c2636cff1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=10.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..04604add4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=12.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..1f64eb844 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=14.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..4663ae101 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=16.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..621103672 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=18.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..a012932e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=20.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..e18147321 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=22.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..e973952a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=String_implementations=8.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..4743b803b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..573dfb276 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..4d8291896 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..d6e02077f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..d3c196db3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..006765b1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..8d8e42827 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..64d72f624 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Guid_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..57211a0d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..61791927b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..c656c5823 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..63c024f93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..41334522a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..a9973faf6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..947501360 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..3d8f7079c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Int_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..808aa1e1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..b43c43bc2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..a5f597967 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..cc5e708dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..88e3b4ab7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..bd92db366 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..eac87be7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..a9ede3da0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Long_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..524d1baa0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..50c06fab1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..e00a5bf38 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..124f315e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..f750c98e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..2e714a152 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..caa39c9fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..423d99574 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=MassTransitNewId_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..6cd28c65f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..1c76f83a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..85a285a9c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..f94025eb4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..93bc13f70 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..290d4302d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..69fee6d06 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..8eab05f8d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..f233af854 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..461a2939e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=12.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..d3722c623 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=14.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..790367922 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..5d769c5a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..b739c632c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=20.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..e664319ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=22.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..9c17ccd75 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=String_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..4f2d67d9d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..10a7438da --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..7375f5040 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=14.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 + + [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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..095ca9db0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..8f5f02845 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..044313f61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..d5578cf3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=22.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 + + [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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..28a85d5a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Guid_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..037a2678a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..f6d976bc2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..9bd235d0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=14.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 + + [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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..f97e1d46c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..6419e0664 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..92d7ad340 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..22b09cc30 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=22.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 + + [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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..6f60230b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Int_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..61fa96ae8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..917e3034c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..3da223b43 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..8e87e8f7b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..9111ff698 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..3e2c7fbab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..f3cbcc71f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..22a32cff2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Long_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..da482c529 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..a4cb794a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..c56a4f0df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..4129aa15b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..6c4f321c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..91628ef17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..c35c74de9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..58ac45afe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=MassTransitNewId_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..213985e24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..918432bc6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..7d3f73837 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..8c12c1da7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..7edb28d90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..0bed25591 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..070a850b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..0c5abc2cc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..0dcb9b128 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=10.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..87bbdc6d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..491d8cd2b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=14.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 + + [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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..bd460364c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=16.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..9b7991c3c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=18.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..defda3c27 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..e0b7578a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=22.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 + + [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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..a27c01c1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=String_implementations=8.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..0a108a310 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=10.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 + + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..59e309d02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=12.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 + + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..8a00e5fdb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=14.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..759a2c075 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=16.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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..8a3ad2b63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=18.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 + + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..991c58751 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=20.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 + + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..464098e91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=22.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..8ed184c05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Guid_implementations=8.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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..ae78422b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..11d33b7a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=12.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 + + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..cc535b858 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=14.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..4f337d939 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..a81a202d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..a7a55e906 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=20.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 + + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..ad31bfdc3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=22.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..f7e27b6e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..bae1dd5e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..b70083c20 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=12.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 + + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..860de4a96 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=14.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..bcda5b8ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..06273bb10 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..bdbcd092c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=20.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 + + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..0c66ac8be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=22.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..77682e716 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..6ab5463a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=10.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 + + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..088068edf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=12.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 + + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..0ad4df066 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=14.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..f76ab3ce3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=16.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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..bccf4cd85 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=18.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 + + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..ef3aa2036 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=20.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 + + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..1e73e3bc0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=22.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..8b13b3fbd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=MassTransitNewId_implementations=8.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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..5da9353d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=10.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 + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..92fa4f5e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..541775636 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..025597f38 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=16.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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..63a553f70 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=18.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 + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..5a7295df7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..37914f310 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..5df4b18bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=NullableString_implementations=8.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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..ec27705b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=10.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..b40bd6386 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=12.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..02751ab1f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=14.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..f8b0acf0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=16.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..b713943c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=18.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..ce5c6f27c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=20.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..3ceea39cc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=22.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..5cc0c8f66 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=String_implementations=8.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..f5ce1c4ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..5c3a7770f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..ee1022fb4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..06d4ea308 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..f5500bff3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..2a3347a70 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..48dfc613e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..3d0554c04 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Guid_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..9ffdb4e02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..4f09cda56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..e427f9a0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..eb1eddec9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..9dbc1e89c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..dec75be45 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..69b37e0d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..5f877ac56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..d2a7d01f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..ef493eeaa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=12.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..8289aee1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=14.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..e477ab0e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..a5d0f71fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..576c61fee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=20.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..85719ea8c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=22.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..804ba3c29 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Long_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..0980d229d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=10.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..d2472deb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..5d6d19ee3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..46fd9ff9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=16.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..4643ba9c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=18.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..412213a93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..2a80192a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..f86a68315 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=MassTransitNewId_implementations=8.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..6ca934bfc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..8ece76653 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=12.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..fca54b9dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=14.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..c915eaf37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..cb6917dec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..9f594c502 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=20.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..9ae6f9fbc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=22.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..c1c796e02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..2f6cb6538 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=10.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..8440c0e91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=12.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..69a25706d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=14.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..20da43188 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=16.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..1a89d3783 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=18.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..2021f4a3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=20.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..908a94335 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=22.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..2d564ebe0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=String_implementations=8.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..31b679e60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=10.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))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..a54e8aa64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..df3330bbd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..ba543d7c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=16.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))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..8fb867a38 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=18.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))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..f22af0935 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..282002bff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..4a49833c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Guid_implementations=8.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))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..924f1b3c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..b8acd39c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..c40ad52ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..fa757b21c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..385bf49db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..8814f7c93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..a25193d7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..91fd0a8ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..4992c1d13 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..756fd32ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..e4d4a49ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..49d1b7d7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..aca0fd0e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..6d1561b2f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..16c48e5fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..abe5dc0af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..245cd0fce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=10.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))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..5dc19196c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..2410686aa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..211f02e0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=16.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))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..66cbd979d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=18.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))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..1685bc792 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..2fb21bf67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..eb1c4a5b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=MassTransitNewId_implementations=8.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))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..de1aaa425 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..b665cdd08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..2cf7a8a0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..3e9ac6672 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..779c1534b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..6f3e276f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..4351f9a1b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..e5fcef399 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..2a17889ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=10.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..c4d132ed7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=12.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..8fd2044c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=14.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..8fb087cbe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=16.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..2745f2d37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=18.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..34d6bd124 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=20.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..598f0a094 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=22.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..a5c3ffdf9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=String_implementations=8.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..77217c1eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..aa0e9d603 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..b0bac6b47 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..1bd20aebe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..7aae723cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..633ed6946 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..c70fa9afe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..82e708483 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Guid_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..2c06e5142 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..4d1d4ed59 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..8d4d6a6d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..5924f76e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..0f1c1c136 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..a0fb84f9b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..3360020df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..7a3df75e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..efef136c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..6c2751aa1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..31c36cb68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..4851e851a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..b3ec23210 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..e11adaaf1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..8207eb5de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..e6e17feda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Long_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..87670c190 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=10.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..64fd0a6f2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..9b45cf838 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..fd47425d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=16.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..f4a89ea46 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=18.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..bf1548e91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..010e86661 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..945a6a3d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=MassTransitNewId_implementations=8.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..be40cd83a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..fe94eedeb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..58ec96550 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..ec284e4c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..8f76c3042 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..7542bb55d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..e98e5e2fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..d65bed611 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..f4912aa65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..8c252bdb1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..e6f20dd29 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..4108d1621 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..5c23ab1ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..0d1ec54cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..c7a1b653c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..0beefbfc3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=String_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..59fdd2d34 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..b99653d61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..f6d92bac0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..ba9e3b4af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..a3b921af1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..b1beb02d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..f87bfddb8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..5a564c7a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..3486c4ed2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..bc3de2107 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..cddac591a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..8023621d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..2c1d64c97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..daa086552 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..f365d516a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..9e07b460e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..720655994 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..28315bad4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..448150649 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..1a1927f06 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..0eab965aa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..b7e2db081 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..4e3a45d74 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..08379d527 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..83148a253 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..cd0987ef0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..5e31a1209 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..9a1692615 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..aac0894fc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..ec151f46b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..08c1a0621 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..b3a165625 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..1634d6ea1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=10.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..81ac7c50f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..d30bffb2d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..aeca40eba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=16.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..4265ec925 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=18.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..55cec33fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..86565e8f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..3050edada --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=NullableString_implementations=8.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..ea79be854 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=10.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..0444ab9df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..d2afa7961 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..5e4f6b6d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=16.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..7a31b0d85 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=18.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..04cf321d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..5ba03b44e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..c76cbc1dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=String_implementations=8.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..2cadbcf4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..db098f640 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..d1358373f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..df9f300f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..bfd3a72cd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..12b40ec32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..182f8fd58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..1d3494bda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..b7ecb065e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..f468d6191 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..707b57126 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..5407b3f36 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..fcc3e8413 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..0c979a147 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..47b17441c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..de5e25a7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..55a4f414c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..c80b78761 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..a1432654e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..ad7c7f227 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..3225066d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..13831d905 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..7c7962f69 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..a563a1b65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Long_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..d142fe9a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=10.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..84d419533 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..079d97892 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..7a8271117 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=16.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..f630adb42 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=18.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..2ae024d88 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..973f2d488 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..22f1e7418 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=MassTransitNewId_implementations=8.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..04db2381e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..9c2a655a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=12.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..57a5c7717 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=14.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..aed062311 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..72589ba78 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..50268cea6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=20.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..416bf55dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=22.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..5f201e22a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..7b225c5ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=10.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..5ee15bf18 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..127f38dc9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..da72da79c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=16.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..a5263215c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=18.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..dd20887aa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..e8ac05b81 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..df35e366e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=String_implementations=8.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..560809a32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..20760cbdd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..9d54af9f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..3bc694f4c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..18c8297a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..e7c321a3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..dd14bedf1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..67200b0ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..7b0460eda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..b87a61f77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..be9b31819 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..7a76f628a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..93617e07e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..c1f819453 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..d249448ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..c4b301a6d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..32466fd03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..f726728dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..85a1d5cbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..3e3f6757a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..adcb7dfc4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..adc4a8e58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..c603603d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..e517dbb7d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..9210cf08a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..bc831ea9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..822ba8d7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..0e73848ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..d8afa4dcf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..f96e20b7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..bd665c866 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..4c3bed820 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..f924373b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..23d99ddfc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..f14be4594 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..48f3004ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..f0b862a26 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..cf8164612 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..b490cea80 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..0e5e0fbd5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..2306c22c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=10.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..95bcfbebf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..b8051c72c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..fdf059ab4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=16.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..c6de44aeb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=18.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..4a4b08aa6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..2ff136b2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..2aafbe864 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=String_implementations=8.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..c9b22c941 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=10.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..996aa33a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=12.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..7585bba25 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=14.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..e43c71a53 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=16.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..5b63c1bde --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=18.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..7c43beecc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=20.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..f8aaf8184 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=22.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..4dc73a3bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Guid_implementations=8.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..18b0a0148 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..d2035a7d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=12.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..d52b38faa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=14.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..729c8f88e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..081cc7fb6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..3157ffa72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=20.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..0594d94e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=22.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..355553184 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..919875a30 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=10.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..42fea307b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..e07dec390 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..8e312ad95 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=16.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..df1a81d04 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=18.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..ef778d495 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..bf206d934 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..a927d4647 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Long_implementations=8.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..f710555fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..5b1fe5f61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..aac7d423a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..98535887a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..e337d0725 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..fb6f293d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..f13b80e12 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..8e088512e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..e96ad705e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=10.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..827979861 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=12.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..3c097cc57 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=14.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..25a72e198 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=16.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..519c4688c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=18.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..205d57699 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=20.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..00ebc555b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=22.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..0a2c70c5d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=NullableString_implementations=8.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..cdbb6962b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=10.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..5736dc3cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=12.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..5fb3564a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=14.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..24ea8d37f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=16.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..f8a161d90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=18.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..030078d56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=20.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..ab4e314f2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=22.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..cd33dbc68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=String_implementations=8.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..021ee3769 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..6eba64861 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..9fb42baf1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=14.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 + + [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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..feeef7fed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..c3dca2453 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..3d293f3dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..2c360bf67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=22.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 + + [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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..343825881 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..18cef3e22 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=10.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..303c1c78a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=12.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..2680eb2a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=14.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..348524f73 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=16.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..fd621c447 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=18.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.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..db7bdc4b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=20.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..19241d482 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=22.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..ed965b5af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Int_implementations=8.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 + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..8bb3eb31a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..b0732c3a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..1e8c06c66 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=14.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 + + [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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..8b2d6174e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..8ffdf5e78 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..b753e71cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..9da573556 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=22.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 + + [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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..e038703bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Long_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..b2fde13db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=10.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..ff0889e6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..e6508a5d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=14.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 + + [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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..d8df2e17b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=16.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..2ea9e0ed8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=18.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.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..45057f8fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..3ad9671fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=22.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 + + [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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..4e5690b33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=MassTransitNewId_implementations=8.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 + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..c92983a00 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..725f20e9b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,158 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..d61cd7dbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,158 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..7772b097c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..34874d2c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..3c07d8621 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,158 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..f28266eeb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,158 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..1fe3e68fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..ab81128f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=10.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..5a70d2574 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=12.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..5f845ccbb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=14.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..b3080b471 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=16.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..b5155aeee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=18.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..360a7047b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=20.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..568769464 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=22.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..67ded5964 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=String_implementations=8.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..7cad7fd07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..5cbb82a47 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..91457fe0c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..5391da48c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..f33c07263 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..797869180 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..6ef8d8afd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..a2c6e95dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..5b1088cec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..c0eb46ad4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..bd2a2a945 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..c0920225c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..6ace944b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..28aeceba1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..0f23ddf60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..e9f639b4f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..077118fde --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..b2bc5e630 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..e3d7c4b86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..5fb60212d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..4390287b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..107e6108c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..4f7207116 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..f0f8115b3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..e6e6b6546 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=10.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..86fd93056 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=12.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..27b992a00 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=14.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..f8793e45d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=16.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..a268e2c5d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=18.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..6bcb80eb6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=20.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..fd93435a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=22.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..ab614c58b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=MassTransitNewId_implementations=8.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..55a95140e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=10.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 + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..bef7dc382 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..c7bfd4f5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..aa3b70f59 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=16.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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..2b751ece2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=18.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 + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..4992c1472 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..f7d66f558 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..45a769730 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=NullableString_implementations=8.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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..fda619149 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=10.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..b16f464c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=12.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 + + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..20fb18a02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=14.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..0995a09c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=16.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..0379e465a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=18.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..1c3474958 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=20.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 + + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..bc8428ca2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=22.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..1b7ff3f73 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=String_implementations=8.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..ef76d2261 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..388503486 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..f8478cf61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..fd521cc26 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..da7aa58f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..b03501580 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..dd26485c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..9dc94f7dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..664735d37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..aec1160a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..ad5d3fb04 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..8d240315e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..953b14f1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..fb7721873 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..5dd494162 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..c11138bfc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..0111fb13b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=10.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..bb7e55516 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=12.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..355b09456 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=14.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..11e4a6734 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=16.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..7194f512d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=18.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..c39b0af6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=20.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..26302a675 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=22.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..c03098328 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Long_implementations=8.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..8d880bba7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=10.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..8aa2d8b37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=12.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..2148537b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=14.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..8c92aaf98 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=16.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..8f328281b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=18.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..dafd9a5fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=20.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..95ab32b7e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=22.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..e5adca609 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=MassTransitNewId_implementations=8.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..420abae28 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..a21732a8c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..19658e87b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..b667b9f36 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..97f8187cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..6d9c63f41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..d713cef72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..177afec77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..93a8b9a75 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=10.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..579dd3551 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=12.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..db2d4f5b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=14.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..a04368558 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=16.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..1a37c9564 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=18.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..20db90e4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=20.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..369fe1021 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=22.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..e610d9914 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=String_implementations=8.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..0336ed8a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..998f8fa2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..e9c697928 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..1ad10b66d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..27094ca7f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..ee5215ac1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..5e38b0cc4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..eca023f58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..261406962 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..90d8d7b19 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..588540031 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..01b74ab39 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..b19d732d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..9865f227c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..b7297d77d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..e98ddada0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..3daccc73e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..1f92a85df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=12.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..afb94a323 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=14.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..09a5b7cf5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..21679f26d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..4ee334a45 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=20.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..3d3e97f9b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=22.verified.txt @@ -0,0 +1,79 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..6bc131c1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..6e5d45ae2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=10.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..50a7eac65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=12.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..eeedb5567 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=14.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..acc645a31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=16.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..79ce64076 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=18.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..c69417c87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=20.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..f44614d4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=22.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..226c21046 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=MassTransitNewId_implementations=8.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..0c6afb572 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..15cef04fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..0248e1f3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..e52fb9d93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..f75710e5d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..0e7c14da2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..536601b10 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..546cb434a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..08cf725c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=10.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))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..635e7f612 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..4c368d859 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..9a4af832e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=16.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))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..9347fa3fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=18.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))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..c29d9ac77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..0eb81ee8e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..2d7e44408 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=String_implementations=8.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))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..72f79342f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..c97274bb4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..30edb16b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..bd94dbea0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..59fbf28ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..332b9e90b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..cf5fbadd3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..f71b40b51 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..67d7a25b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..4b84520f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..879a520ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..cf99466f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..af6bef068 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..8bc08f3a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..136b32377 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..778ee7d6e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..4109b13dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..764f08fcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..6f0a2cdad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..bd4fa34a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..cedec3e8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..854b3cc21 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..3c959b695 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..3519a7283 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Long_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..fc3b18065 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..8651c4dad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..7ac50f903 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..6d34ff65e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..bfc7d96f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..af1606f35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..55126c1ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..6dcb296a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=MassTransitNewId_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..5bd4626f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..a8180b4b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..f54a46f5e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..1cd57f302 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..11e46e4d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..84452a0ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..016e482cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..941424735 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..47e829d31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=10.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..a9a88eda8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=12.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..d47937017 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=14.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..06aa9a38a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=16.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..9758d5d49 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=18.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..f9d033395 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=20.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..c4b9eb016 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=22.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..10e2fca19 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=String_implementations=8.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..21a4aff6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..bd1a0c1b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..761d8b48d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..8e953bfaa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..1cbefdf63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..d2d18858c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..4b9aac707 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..50dc2163e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..fe2552cfc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..a6452e569 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..2f470a7f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..22e321b6d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..91ebe8562 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..1ef059281 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..7a5192788 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..23df2b215 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..e253e0171 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..c4728a40a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..213572afb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..696bdc193 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..92b7b1a63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..ef7989c64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..b978f39f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..83868c3cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..cb7dc4537 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=10.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..04bb57e6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=12.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..c01e6b37e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=14.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..3bdb5713c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=16.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..9b3d7766c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=18.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..7dd0dfa32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=20.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..a3b9dce8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=22.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..0e93c2df1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=MassTransitNewId_implementations=8.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..1e87daeba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=10.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..6abbd06af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..689b1852f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..d2252b057 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=16.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..38515c7e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=18.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..abe820325 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..888933a09 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,96 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..1f149cadb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=NullableString_implementations=8.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..025b6d227 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=10.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..a3cca1b8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=12.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..a18cc45b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=14.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..ff9a8935f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=16.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..0406de09f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=18.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..e18adf568 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=20.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..479082ce5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=22.verified.txt @@ -0,0 +1,87 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..404917261 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=String_implementations=8.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..821e4a566 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..cac6eeb56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..d451fc672 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..0675f196a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..ccba9424a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..93bc09281 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..1e61a8fc1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..a4aafd37f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..65a8dc3f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..4100d1041 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..52eaad7e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..d3a83f47e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..e85591750 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..be3adca82 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..de39de512 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..513369a74 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..c30900e4c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..c4ca3d062 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..ce5f11c6f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..72308597d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..3d46b1db6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..26f053dc1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..c1e3c4c1d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..d1d147597 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Long_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..737b6901a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..e4b9586ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..83235bdd4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..0f4dbf867 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..5b9e5e6d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..3e199eaa5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..33e18e2bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,121 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..571087579 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=MassTransitNewId_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..2cf1bf8ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..9c1ea34d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..91088d82f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..098d7ebd2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..3eb3238e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..7a131ac2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..b9e052f3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..857a38079 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..998a22a4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..e7dd4bd8c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=12.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..fccb1c73a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=14.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..3e8fb021b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..c7d5b53eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..8570a024f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=20.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..ae7434462 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=22.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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..5d0022851 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=String_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..5359000f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..45b87d6fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..94c8aebb3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..583f3fc91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..e30620226 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..aa8616e32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..3a596015c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..d7ec25efd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..093e6bebc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..81b1cecb1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..702337128 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..d5386bdae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..93a23f9c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..207334b6d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..4611f2b6e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..591baaa9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..52fde8f08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=10.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..70648074b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..7a6f7b1e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..314f2ed90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=16.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..65df58448 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=18.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.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..c97169cd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..f77de3bdb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..062a0ca71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Long_implementations=8.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 + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..1e4f37c07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=10.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..20eed6e64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=12.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..249e279e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=14.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..7b62750ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=16.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..93026e033 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=18.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..fcb23ccbc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=20.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..e0b1ba1fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=22.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..779594ac1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=MassTransitNewId_implementations=8.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..0d33c0902 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..a3bcf7467 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..9e2625558 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..5bccd0d41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..09f3ded68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..a8439037c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..310915ed5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..73a5f7048 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..d68c1a01c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=10.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..c0cee29c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..96e5626a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..af0956977 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=16.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..c9de0dda3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=18.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..57d2f0d3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..4b30aafda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..4d6fd15f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=String_implementations=8.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..6d39ee51e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=10.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..83adabb4f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=12.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..273169006 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=14.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..5098c1970 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=16.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..59f9065b3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=18.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..7c3dcb643 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=20.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..29fa8ac57 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=22.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..add911605 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Guid_implementations=8.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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..be8fdc6ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=10.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..2f2320545 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=12.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..0ec3c048b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=14.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..89c67f844 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=16.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..744b954e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=18.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..17bf7c7df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=20.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..e29927ffe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=22.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..9e7835d8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Int_implementations=8.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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..e6c8cc274 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..a0ae3904f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..a15289f44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=14.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 + + [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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..e7010af0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..1c0e9e4f2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..94284ffef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..2ad02d135 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=22.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 + + [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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..695860243 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Long_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..afca2cb04 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..c97f1566b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..38a626d96 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..0fb39c280 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..df6af121d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..8624d75d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..78eda30f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..cde81cc34 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..11dc9b9a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..e2d98a756 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,166 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..29192eba6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,166 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..67d0eb1ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..5fd28f5e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..93aebdde9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,166 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..6fa345142 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,166 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..11199e274 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..d2d946cbe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..b9855510a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=12.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..f35b3acfc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=14.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..66a887bfa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..12993ce5e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..11cdec5b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=20.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..d5e0a9c0a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=22.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..787516a13 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=String_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..b9197dd97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..3604ff819 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..0cbdba62d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..daf2d3456 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..f833901e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..da042352e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..4b91649ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..9d66f6ad0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..4f9a2dbc8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..849de31af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..2513e1e93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..f2516d327 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..8a8418e33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..13feaa379 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..b1988d168 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..fa4bbb5ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..e7a76e920 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=10.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..d4baaa746 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=12.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..86dfabe80 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=14.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..3f6ed7e36 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=16.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..36f4da9a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=18.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..dc45dc1fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=20.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..ee028571d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=22.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..4857da1e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Long_implementations=8.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..af2e4b4af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..f5693fd49 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..b510f411d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..dec0cebe3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..774702c2b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..04eec2807 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..6627204f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..5d12c20c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..7c0293120 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..18d55262b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=12.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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..db8414486 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=14.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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..17b4eb376 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..ea2d48fa0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..8b16c831a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=20.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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..c4d1d78b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=22.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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..c20e4f72d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..27051f3a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=10.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 + + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..57f81be17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=12.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..e8b0e8a86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=14.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..83513b1bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=16.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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..6168b73b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=18.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 + + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..cddb148d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=20.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..7a4b075a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=22.verified.txt @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..c82af88f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=String_implementations=8.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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..e136f7cd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..37af6b195 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..9a2efec24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..46f86a633 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..bb6d64db0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..3effdd55f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..fc1c4f5cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..aa4c2cb2b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Guid_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..5a432e194 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..6cbe158e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..d44fc8d47 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..04c0f8df7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..c46b69e3f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..e8e117b8a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..c756445bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..24b889d8e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..5c6aa2db3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..daf5ead96 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..cf4ec22ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..83725e25f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..abbb6951b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..fb46fa213 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..336bdeb63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..8ce3e9e87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..e6fa64928 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..2babb85a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..73de2f96a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..b092f3792 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..4d9a6c508 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..4506e0618 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..90b93b1c6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..293c459a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=MassTransitNewId_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..f40bcea43 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..d314e1e0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..e2ac9ebcf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..283ddefe5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..669e2d2b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..a75fbbddf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..2345a9eba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..8aefc99bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..028b888fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=10.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..758f63f69 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=12.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..a5e10d6c6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=14.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..99ae579c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=16.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..411287c8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=18.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..8b1c9fabc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=20.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..c3c6824be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=22.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..10ff503bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=String_implementations=8.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..a7f91162b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..172649140 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..708202b5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..df0fa323d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..adfb2b96e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..87512d7ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..1902f1712 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..65b4ca96a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..05647e921 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..14df15f9b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..a2b211962 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..3619e77cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..bb1bf5859 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..fb93c6610 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..0751b5eda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..ec30bf754 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..6856bcad7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..907636278 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..b8cc20f7e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..f22b127b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..88c15e63e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..2d69ed3af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..ed6b57768 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..af352c3f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Long_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..63f278f55 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..4e6ade3d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..1709fbc7b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..f081368a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..169abdc53 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..f3bdb8665 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..aaed7c85e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..de3e10ab0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..83cb66628 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=10.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..3c45fbf13 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..77ad5f6b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..55d596db6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=16.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..e6f72d7ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=18.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..c91e9883e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..032804a8a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,129 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..57ce29b49 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=NullableString_implementations=8.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..d57683723 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..5bbd30f01 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=12.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..a0299672c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=14.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..eaa9ea83e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..965cb76b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..ee56085d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=20.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..988c7dfb3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=22.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..05ec407e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=String_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..e417f2bb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..dd9bd5371 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..8fcf193a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..47d8766e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..da3f9debe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..4edbecae8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..89b4594e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..998033f26 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..b217a18f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..0f23ece05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..d95db8c1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..bbb6afcad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..f3da32342 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..a02910582 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..7894444ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..e7f4cbcaf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..5a3a8931e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=10.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..7585895fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=12.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..64739a4d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=14.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..46e9e0379 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=16.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..4a33447fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=18.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..7187b47de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=20.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..bc428bf19 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=22.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..c42bdc0f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Long_implementations=8.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..50c1f69da --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..0c8a7a192 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..a1bbbfaf0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..384781a7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..3d2bfe481 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..158225aa5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..1fcdedee0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..6c1ba8720 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..f882fa521 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..95ca57c4c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..826b7341c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..2a2325321 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..7aa99342e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..97f7cd732 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..2375c1de7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..74b75066d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..e18cf69a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..92c44fdf3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=12.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))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..9bb74e20d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=14.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..49c172c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..b98bdc99b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..653982a03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=20.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))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..865479e9c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=22.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..7cf141d3d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=String_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..2894955f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..c7f4b2bf6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..4ca03e340 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..8590c16fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..bb64861ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..94c4de992 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..6f9be255c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..6417a1033 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..02414d536 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..db246f064 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..0ca0b4339 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..2cea27237 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..1bd44e884 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..3b33dddd0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..efae95dda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..139884a16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,130 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..6714542e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..86f46c7cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..7c82cfa71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..7a3728710 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..c300bf935 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..640fba95b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..00a46c5c6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..0dd95ad01 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Long_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..af8b7ab4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..c66d64de9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..cac0e5853 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..237c813ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..d780070d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..dd6673793 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..65fc1f601 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,139 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..d01ce3726 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..34ffd95fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..b22ca0893 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..6cc1fce56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..749e73e6c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..7bc9546b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..398146f28 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..d81f7dee6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..d72dd59e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..f5205c831 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=10.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..9410e5d47 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=12.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..bf394ef58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=14.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..686d900f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=16.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..0cf20512e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=18.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..246209120 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=20.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..215b8ee24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=22.verified.txt @@ -0,0 +1,142 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..5cc4937d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=String_implementations=8.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..02021bfcc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..c98b6cc8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..299217ea9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..2430501c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..c2cffb2df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..a2ac7ce1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..541ae5dcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..cf0922b1d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Guid_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..99cdcaad2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..2be69aef8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..7bf61c7e5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..a2943d33c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..52956e24f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..c0b5ac3cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..cad87ab54 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..8acd70883 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Int_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..12ccc5162 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=10.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..14b3dfa96 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=12.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..dc2fac9a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=14.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..a3179b84a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=16.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..cf21ee0ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=18.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..e72c9a8b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=20.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..1fcac2a7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=22.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..d28a6deb6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Long_implementations=8.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..eccf4190e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..cb744f7c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..51458cbc6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..490146756 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..c811e354e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..920ffb2f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..70323c011 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..e9a43f2ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=MassTransitNewId_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..6e3060b0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..aa916cbe7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=12.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..25d20592b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=14.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..4eabe423d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..7438335c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..b15125857 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=20.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..3afbdd340 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=22.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..710af4815 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,97 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..8ab0c0e34 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=10.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..a570a318d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=12.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..4edc85290 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=14.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..4d7c1f5a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=16.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..f6c704da1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=18.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..b4b89e5d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=20.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..c319b3ad8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=22.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..09ad3760f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=String_implementations=8.verified.txt @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..1dd0ce6ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..3a80105a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..b64d30643 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..d76a215f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..8603a10d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..9a2299d5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..e4dce4b37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..ba3ac89d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Guid_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..6488f60a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..0bd6e86d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..041263dde --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..ff8f52490 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..f419cbe43 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..64cc19849 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..88fa7340e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..46d90c695 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Int_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..60cc50e58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..eb6e791d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..b0c942012 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..b9776be24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..aae7be335 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..3cf9de3b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..617639fd0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..6958a24a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..5d6ddee07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..a427b8165 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..e646030bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..7911c8ee3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..2cc9826f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..82627701a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..84e7759d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..854b7aa95 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=MassTransitNewId_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..9f3727669 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..fc1e0a127 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..b75387db0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..3141f9e0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..b46ec44c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..4d298c90b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..cee8c6262 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,150 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..ec950e5c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..a93202182 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=10.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..504ee06ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..50cd36208 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..8db115f9e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=16.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..d60eef085 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=18.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..676f0cfd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..6eea277b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..f9aa5f741 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=String_implementations=8.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..734ce0bba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..831a77143 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..e948264bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..5d458f039 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..2578dcaea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..e2abb57a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..a46177fa6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..261923214 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Guid_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..8bf7f7180 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..af4287dcb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..10f4510e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..16e3795e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..b040473b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..709d747e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..28a5d5d63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..921175eac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Int_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..765f4c018 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=10.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..91bb205f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=12.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..3441617c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=14.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..ebf885a09 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=16.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..b523129f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=18.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..83f60afa5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=20.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..79dfa0ac4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=22.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..990cfe5e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Long_implementations=8.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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..0ceeeecd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..39cae6cad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..4e6ed5819 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..91191fd61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..5e91b137a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..1883519de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..91c809250 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..a68af9946 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=MassTransitNewId_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..89550ad5a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..11b3ad321 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=12.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..c654aefe7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=14.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..9266424bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..59a2ed2e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..6ed717ca3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=20.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..4c6a4ae9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=22.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..59de3b067 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..b5ad9cf07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=10.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..86222a61c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=12.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..f5264f5d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=14.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..41a35f98e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=16.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..a8f8d04d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=18.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..9edd3c49c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=20.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..9484a33ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=22.verified.txt @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..9ea905a14 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=String_implementations=8.verified.txt @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..f183a5355 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=10.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..da9953bc9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=12.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..befef49f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=14.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..7fe6816ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=16.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..6f0153cd6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=18.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..838a30c73 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=20.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.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..3c6c5ae40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=22.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.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..391100217 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Guid_implementations=8.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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 + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..6627c0d99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..3d8cdbcf6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=12.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..13ae9a8f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=14.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..b36e7e729 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..e481aef23 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..dcf47428b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=20.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.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..4878e4ee1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=22.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.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..a7e1706aa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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 + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..b6acd0100 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=10.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..2997c0b63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..ee6dd0145 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=14.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 + + [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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..3df8f1d21 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=16.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..6566fcdf0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=18.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..bee9669ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..ca71df69a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=22.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 + + [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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..03aca80fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Long_implementations=8.verified.txt @@ -0,0 +1,147 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..785d9c61e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,152 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..1cea38bdb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=12.verified.txt @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..cc5066a65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=14.verified.txt @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..7ee51c4fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,152 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..0161957a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,152 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..24abfe549 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=20.verified.txt @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..7fa313d2f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=22.verified.txt @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..009044420 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,152 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(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(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..7e353e19b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,167 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..98217638b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,177 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..055010a2c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,177 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..06c979f73 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,167 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..287d05ec2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,167 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..3f5a898a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,177 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..c3cf146fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,177 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [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 string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..479e411ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,167 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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 + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..c97197451 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=10.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..daf3ad622 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=12.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..a1440dd1f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=14.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..2886e09c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=16.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..a20e895f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=18.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.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..edb76f876 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=20.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..2ec323d8a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=22.verified.txt @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..1d9ac8031 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=String_implementations=8.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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 + { + string stringValue => new MyTestId(stringValue), + _ => 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(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + 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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..26ae8fc92 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..783567cec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..dd958de02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..6b831849f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..65dc3d597 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..233ffab17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..9b77bd803 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..c353206e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Guid_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.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(System.Guid) || sourceType == typeof(string) || 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(System.Guid)) + { + 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 guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..cce8122a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=10.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..49b1446ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..56574789c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..1b46b3de0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=16.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..f45a3df89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=18.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..33eb32ca6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..bfd1f8eee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..7a0d47298 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Int_implementations=8.verified.txt @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.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(int) || sourceType == typeof(string) || 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(int)) + { + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..e9ed58a0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=10.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..2607e902a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=12.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..d67012c36 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=14.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..9cb79cd5b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=16.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..745ea3c10 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=18.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..c4f459aa8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=20.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..658a12753 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=22.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..457510ee4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Long_implementations=8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || 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 + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.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(long) || sourceType == typeof(string) || 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(long)) + { + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..d2f39338b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=10.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..a4faacfbb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=12.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..50f8e1295 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=14.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..4ba00981b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=16.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..af1402482 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=18.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..e550701d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=20.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.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..9c59f8264 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=22.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.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..71075798a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=MassTransitNewId_implementations=8.verified.txt @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || 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(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().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.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..71f8b5552 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=10.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..229a0fb31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=12.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..df4a9dc16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=14.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..d29ace3be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=16.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..c5c31e304 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=18.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..77aef0423 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=20.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..3ef160179 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=22.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..462416287 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=NullableString_implementations=8.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..a2dac5d39 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=10.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..dc1d5eccb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=12.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..28dcbf3b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=14.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..be545ecf4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=16.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..5bd33c95c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=18.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..7620a0319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=20.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..cdd837900 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=22.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..8bf5a16b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=String_implementations=8.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || 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(string)) + { + return idValue.Value; + } + } + + 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) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=10.verified.txt new file mode 100644 index 000000000..6232df97b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=12.verified.txt new file mode 100644 index 000000000..49d49f8b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=14.verified.txt new file mode 100644 index 000000000..b7d9f987f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=16.verified.txt new file mode 100644 index 000000000..50a4140d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=18.verified.txt new file mode 100644 index 000000000..a2bb51700 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=20.verified.txt new file mode 100644 index 000000000..a8214f873 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=22.verified.txt new file mode 100644 index 000000000..e0ae16440 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator System.Guid(MyTestId id) => id.Value; + public static implicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=8.verified.txt new file mode 100644 index 000000000..8dc6348d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Guid_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator System.Guid(MyTestId id) => id.Value; + public static explicit operator MyTestId(System.Guid value) => new(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(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=10.verified.txt new file mode 100644 index 000000000..e5b7c08b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=12.verified.txt new file mode 100644 index 000000000..f38e90063 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=14.verified.txt new file mode 100644 index 000000000..941e1eeaa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=16.verified.txt new file mode 100644 index 000000000..791230cba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=18.verified.txt new file mode 100644 index 000000000..e2bc4632a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=20.verified.txt new file mode 100644 index 000000000..f6ac8cb42 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=22.verified.txt new file mode 100644 index 000000000..e5e4bdda3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator int(MyTestId id) => id.Value; + public static implicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Int_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=8.verified.txt new file mode 100644 index 000000000..f1818b545 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Int_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator int(MyTestId id) => id.Value; + public static explicit operator MyTestId(int value) => new(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.GetInt32()); + } + + 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=Long_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=10.verified.txt new file mode 100644 index 000000000..19fe27700 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=12.verified.txt new file mode 100644 index 000000000..c8fe12340 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=14.verified.txt new file mode 100644 index 000000000..3c8df60f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=16.verified.txt new file mode 100644 index 000000000..d2303190b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=18.verified.txt new file mode 100644 index 000000000..fb7f5e9cc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=20.verified.txt new file mode 100644 index 000000000..75c9f4ce0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=22.verified.txt new file mode 100644 index 000000000..5c06d6e45 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator long(MyTestId id) => id.Value; + public static implicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=Long_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=8.verified.txt new file mode 100644 index 000000000..819287481 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Long_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator long(MyTestId id) => id.Value; + public static explicit operator MyTestId(long value) => new(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.GetInt64()); + } + + 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=MassTransitNewId_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=10.verified.txt new file mode 100644 index 000000000..906d615a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=12.verified.txt new file mode 100644 index 000000000..8f8707500 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=12.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=14.verified.txt new file mode 100644 index 000000000..60b378330 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=14.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=16.verified.txt new file mode 100644 index 000000000..4de2acc2d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=18.verified.txt new file mode 100644 index 000000000..6e8a7c0d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=20.verified.txt new file mode 100644 index 000000000..9c5b11153 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=20.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=22.verified.txt new file mode 100644 index 000000000..d1305d0d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=22.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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 static implicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static implicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=8.verified.txt new file mode 100644 index 000000000..f4b18e386 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=MassTransitNewId_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator MassTransit.NewId(MyTestId id) => id.Value; + public static explicit operator MyTestId(MassTransit.NewId value) => new(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(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=10.verified.txt new file mode 100644 index 000000000..9c19c7315 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=10.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=12.verified.txt new file mode 100644 index 000000000..a821f7d4e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=12.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=14.verified.txt new file mode 100644 index 000000000..cbcd3084a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=14.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=16.verified.txt new file mode 100644 index 000000000..f5d2654ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=16.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=18.verified.txt new file mode 100644 index 000000000..8f28af608 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=18.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=20.verified.txt new file mode 100644 index 000000000..7d9b89710 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=20.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=22.verified.txt new file mode 100644 index 000000000..6fe34b902 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=22.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string?(MyTestId id) => id.Value; + public static implicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=8.verified.txt new file mode 100644 index 000000000..815ffae40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=NullableString_implementations=8.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string?(MyTestId id) => id.Value; + public static explicit operator MyTestId(string? value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=10.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=10.verified.txt new file mode 100644 index 000000000..81a9e5031 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=10.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=12.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=12.verified.txt new file mode 100644 index 000000000..18e856935 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=12.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=14.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=14.verified.txt new file mode 100644 index 000000000..66d8005ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=14.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=16.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=16.verified.txt new file mode 100644 index 000000000..a9b043752 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=16.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=18.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=18.verified.txt new file mode 100644 index 000000000..cd65ba1a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=18.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=20.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=20.verified.txt new file mode 100644 index 000000000..ad523c0d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=20.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.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=22.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=22.verified.txt new file mode 100644 index 000000000..8b91a6190 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=22.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.IComparable, System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + public static implicit operator string(MyTestId id) => id.Value; + public static implicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=8.verified.txt new file mode 100644 index 000000000..196ddddb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=String_implementations=8.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public static explicit operator string(MyTestId id) => id.Value; + public static explicit operator MyTestId(string value) => new(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.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs b/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs index de4b30a08..3d6681ee5 100644 --- a/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs +++ b/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs @@ -114,11 +114,50 @@ public Task GeneratesIdWithNestedClassCorrectly(int nestedClassCount) .UseParameters(nestedClassCount); } + [Theory] + [MemberData(nameof(ParametersOfImplicitAndExplicitCast))] + public void AllCombinationsOfExplicitAndImplicitCastAlwaysThrow( + string ns, + StronglyTypedIdConverter converter, + StronglyTypedIdBackingType backingType, + StronglyTypedIdImplementations implementations) + { + const string idName = "MyTestId"; + Assert.Throws(() => + SourceGenerationHelper.CreateId( + idName: idName, + idNamespace: ns, + parentClass: null, + converters: converter, + backingType: backingType, + implementations: implementations + )); + } + public static IEnumerable Parameters() { foreach (var converter in EnumHelper.AllConverters(includeDefault: false)) foreach (var backingType in EnumHelper.AllBackingTypes(includeDefault: false)) - foreach (var implementations in EnumHelper.AllImplementations(includeDefault: false)) + foreach (var implementations in EnumHelper.AllImplementations( + includeDefault: false, + skip: new[] + { + StronglyTypedIdImplementations.ExplicitCast|StronglyTypedIdImplementations.ImplicitCast, + })) + foreach (var ns in new[] { string.Empty, IdNamespace }) + yield return new object[] { ns, converter, backingType, implementations }; + } + + public static IEnumerable ParametersOfImplicitAndExplicitCast() + { + foreach (var converter in EnumHelper.AllConverters(includeDefault: false)) + foreach (var backingType in EnumHelper.AllBackingTypes(includeDefault: false)) + foreach (var implementations in EnumHelper.AllImplementations( + includeDefault: false, + only: new[] + { + StronglyTypedIdImplementations.ExplicitCast|StronglyTypedIdImplementations.ImplicitCast, + })) foreach (var ns in new[] { string.Empty, IdNamespace }) yield return new object[] { ns, converter, backingType, implementations }; }